#include "StdAfx.h"
#include ".\clientsocket.h"

ClientSocket::ClientSocket(void)
{
	m_sock = -1;
	memset ( &m_addr, 0, sizeof ( m_addr ));

}

ClientSocket::~ClientSocket(void)
{
}

bool ClientSocket::Create(void)
{
	m_sock = socket ( AF_INET, SOCK_STREAM, 0 );

	if ( ! Is_Valid() ) return false;
	int on = 1;
	if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 ) return false;
	return true;
}

bool ClientSocket::Connect(string host, int port)
{
	if ( ! Is_Valid() ) return false;
	m_addr.sin_family = AF_INET;
	m_addr.sin_port = htons ( port );
	m_addr.sin_addr.S_un.S_addr = inet_addr(host.c_str());

	int status = connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) );

	if ( status == 0 )
		return true;
	else
		return false;
}

int ClientSocket::Send(char * ptr, int len)
{
	return send( m_sock, ptr, len, 0 );
}

int ClientSocket::Receive(char * buffer, int len)
{
	return recv ( m_sock, buffer, len, 0 );
}

void ClientSocket::Close(void)
{
	closesocket(m_sock);
}
